package com.dzm.config;//package com.dzm.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**
 * 作者：Dzm
 * 时间：2024-08-16-9:29
 * 功能：
 */

@Configuration
@EnableSwagger2
// 通过yaml中swagger2.enable控制是否加载swagger
//@ConditionalOnProperty(prefix = "swagger2", value = "enable", havingValue = "true")
public class SwaggerConfig {

//    @Bean
//    public static BeanPostProcessor springfoxBeanPostProcessor() {
//        return new BeanPostProcessor() {
//            @Override
//            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//                if (bean instanceof WebMvcRequestHandlerProvider) {
//                    getHandlerMappings(bean).removeIf(m -> m.getPatternParser() != null);
//                }
//                return bean;
//            }
//
//            private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
//                try {
//                    Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");//固定写法不用改
//                    field.setAccessible(true);
//                    return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
//                } catch (IllegalAccessException e) {
//                    throw new IllegalStateException(e);
//                }
//            }
//        };
//    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .paths(PathSelectors.ant("/api/**"))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .termsOfServiceUrl("admin api docs")
                .description("后台管理系统-API测试")
                .version("0.0.1")
                .build();
    }
}